<template>
  <a class="download-btn btn-main" id="install-btn" :class="{ 'installing': isInstalling }">
    <span>{{ installText }}</span>
  </a>
</template>

<script setup>
import { onMounted, ref } from 'vue';

const props = defineProps({
  apkPath: {
    type: String,
    default: ''
  }
});

const isInstalling = ref(false);
const installText = ref('Download the app');

const downloadApk = () => {
  if (props.apkPath) {
    // 创建一个临时的a标签来触发下载
    const link = document.createElement('a');
    link.href = props.apkPath;
    link.download = 'app.apk'; // 设置下载文件名
    link.target = '_blank';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  } else {
    console.warn('APK path is not provided');
  }
};

onMounted(() => {
  const installButton = document.getElementById('install-btn');
  const mask = document.getElementById('mask');
  const maskTip = document.getElementById('mask-tip');

  window.addEventListener('beforeinstallprompt', (event) => {
    event.preventDefault();
    window.deferredPrompt = event;
    installButton.style.display = "block";
  });

  installButton.addEventListener('click', async () => {
    if (window.deferredPrompt) {
      // Show PWA install prompt
      isInstalling.value = true;
      installText.value = 'Installing...';
      
      window.deferredPrompt.prompt();
      const { outcome } = await window.deferredPrompt.userChoice;
      
      if (outcome === 'accepted') {
        console.log('User accepted the install prompt');
      } else {
        console.log('User dismissed the install prompt');
      }
      
      window.deferredPrompt = null;
      isInstalling.value = false;
      installText.value = 'Download the app';
    } else {
      // 直接下载APK文件
      isInstalling.value = true;
      installText.value = 'Downloading...';
      
      try {
        downloadApk();
        setTimeout(() => {
          isInstalling.value = false;
          installText.value = 'Download the app';
        }, 2000);
      } catch (error) {
        console.error('Download failed:', error);
        isInstalling.value = false;
        installText.value = 'Download failed';
        setTimeout(() => {
          installText.value = 'Download the app';
        }, 3000);
      }
    }
  });

  window.addEventListener('appinstalled', () => {
    installButton.style.display = "none";
    console.log('App was installed successfully');
  });
});
</script>

<style scoped>
.download-btn {
  display: inline-block;
  padding: 12px 24px;
  background-color: #FE2C55;
  color: white;
  border-radius: 4px;
  font-weight: bold;
  text-decoration: none;
  margin: 10px 0;
  transition: background-color 0.3s ease;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(254, 44, 85, 0.4);
  text-align: center;
  min-width: 180px;
  width: 100%;
  max-width: 280px;
  box-sizing: border-box;
}

.download-btn:hover {
  background-color: #e6254d;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(254, 44, 85, 0.5);
}

.download-btn:active {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(254, 44, 85, 0.4);
}

.installing {
  background-color: #999;
  pointer-events: none;
  opacity: 0.8;
}
@media (max-width: 480px) {
  .download-btn {
    padding: 10px 20px;
    min-width: 150px;
    font-size: 14px;
  }
}
</style>
